package basicType;

class Professor implements Cloneable
{
    String name;
    int age;
    Professor(String name,int age)
    {
        this.name=name;
        this.age=age;
    }
    public Object clone()
    {
        Object o=null;
        try
        {
            o=super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println(e.toString());
        }
        return o;
    }
}
public class Student implements Cloneable
{
    String name;
    int age;
    Professor p;
    Student(String name,int age,Professor p)
    {
        this.name=name;
        this.age=age;
        this.p=p;
    }
    public Object clone()
    {
        Student o=null;
        try
        {
            o=(Student)super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println(e.toString());
        }
        //对引用的对象也进行复制
        o.p=(Professor)p.clone();
        return o;
    }
    public static void main(String[] args) {
        Professor p=new Professor("wangwu",50);
        Student s1=new Student("zhangsan",18,p);
        Student s2=(Student)s1.clone();

        System.out.println(s1.p == s2.p); //false

        System.out.println(s1.name == s2.name); // true

        System.out.println(s1.p.name == s2.p.name); //true

        s2.p.name="lisi";
        s2.p.age=30;
        //学生1的教授不 改变。
        System.out.println("name="+s1.p.name+","+"age="+s1.p.age);
        System.out.println("name="+s2.p.name+","+"age="+s2.p.age);

        System.out.println(s1.name == s2.name); //true

        System.out.println(s1.p.name == s2.p.name); //false

        String x = "123";

        String y = "123";

        System.out.println(x == y);

        x = "456";

        System.out.println(x == y);


    }
}
